angular.module('starter.controllers', [])

//暂时用作用户详情
.controller('ChatsCtrl', function($scope, dataService) {
  // With the new view caching in Ionic, Controllers are only called
  // when they are recreated or on app start, instead of every page change.
  // To listen for when this page is active (for example, to refresh data),
  // listen for the $ionicView.enter event:
  //
  //$scope.$on('$ionicView.enter', function(e) {
  //});

    var requestUrl="getUserInfo";
    var userInfoParams={
      reqHead:{
        businessId:APIS.businessId,
        userId:APIS.userId,
        registrationId:APIS.registrationId,
        registerType:APIS.registerType
      },
      reqBody:{
        content:{

        }
      }
    }
    dataService.getData(requestUrl,userInfoParams).then(function(obj){
      if(obj.data.resultCode ==1){
        //$scope.user_Icon=obj.data.resultData.icon;
        $scope.user_name=obj.data.resultData.name;
        $scope.user_account=obj.data.resultData.account;
        if(obj.data.resultData.gender==1){
          $scope.user_gender="男";
        }else{
          $scope.user_gender="女";
        }
        $scope.user_age=obj.data.resultData.age;
        $scope.user_address=obj.data.resultData.address;
        $scope.user_comment=obj.data.resultData.comment;
        $scope.user_identityId=obj.data.resultData.identityId;
      }else{
        alert(obj.resultData);
      }
    });
})
//注册控制器
  .controller('registerCtrl',function($scope,dataService,$interval){

    $scope.vDisabled=false;
    $scope.vtext="获取验证码";
    $scope.number=60;
    $scope.registerDisabled=true;
    document.getElementById("registerBtn").style.backgroundColor="gray";

    var idInput=document.getElementById("registerName");
    var pwdInput=document.getElementById("registerPwd");
    var againPwdInput=document.getElementById("registerPwdAgain");
    var verificationInput=document.getElementById("vInput");
    var checkBox=document.getElementById("radio");

    //定义input框文本改变时触发的事件
      function changed(){
      if(idInput.value.length==0||pwdInput.value.length==0||againPwdInput.value.length==0||verificationInput.value.length==0||checkBox.checked==false){
        $scope.registerDisabled=true;
        document.getElementById("registerBtn").style.backgroundColor="gray";

      }else{
        $scope.registerDisabled=false;
        document.getElementById("registerBtn").style.backgroundColor="orange";

      }
    }

    $scope.inputChanged=changed;

    var registerUrlParams={
        reqHead:{
          businessId:APIS.businessId,
          //userId:$scope.userId,
          userId:"1",
          passwd:"2",
          //passwd:md5.create($scope.passwd),
          // passwd:$scope.passwd,
          registrationId:APIS.registrationId,
          registerType:APIS.registerType,
          verificationCode:'1'
        },
      reqBody:{
        content:{

        }
      }
    };
    var requestUrl="registerUser";

    //注册按钮点击事件
    $scope.register=function(){
      dataService.getData(requestUrl,registerUrlParams).then(function(obj){
console.log(obj);
console.log(obj.data.result_code);
alert(obj.data.result_code);
      })
    };
    //设置验证码按钮点击事件
    $scope.verificationClicked=function(){

      var time=$interval(function(){
        //设置按钮状态为不可点击
        $scope.vDisabled=true;
        document.getElementById("vBtn").style.backgroundColor="gray";

        $scope.number--;
        console.log($scope.number);
        //通过数据的双向绑定，改变按钮的文本值
        $scope.vtext=$scope.number+"秒后重发";
        if($scope.number==0){
          $interval.cancel(time);
          $scope.vtext="获取验证码";
          $scope.number=60;
          $scope.vDisabled=false;
          document.getElementById("vBtn").style.backgroundColor="dodgerblue";

        }
      },1000);
    }
    //服务条款选择框
    $scope.radioClicked=function(){
      var radioBtn=document.getElementById("radio");
      changed();
      if($scope.radioState==false){
        $scope.radioState=true;
 console.log("111");
      }
      else{
        $scope.radioState=true;
console.log("222");
      }
    }

  })
  //登录控制器
  .controller('loginCtrl',function($scope,dataService,$state){

    var loginUrlParams={
      reqHead:{
        loginName:$scope.loginName,
        loginPwd:$scope.loginPwd,
        registrationId:APIS.registrationId,
        registerType:APIS.registerType,
        isComeFromContract:APIS.isComeFromContract
      },
      reqBody:{
        content:{

        }
      }
    };
    var requestUrl="mobileAction_login";

    $scope.loginDisabled=true;
    var loginBtn=document.getElementById("login");
    loginBtn.style.backgroundColor="gray";

    var nameInput=document.getElementById("loginName");
    var pwdInput=document.getElementById("loginPwd");

    function changed(){
      if(nameInput.value.length!=0&&pwdInput.value.length!=0){
        $scope.loginDisabled=false;
        loginBtn.style.backgroundColor="orange";

      }else{
        $scope.loginDisabled=true;
        loginBtn.style.backgroundColor="gray";
      }
    }
    $scope.inputChanged=changed;
//登录按钮响应函数
    $scope.signIn=function(){

      dataService.getData(requestUrl,loginUrlParams).then(function(obj){

console.log(obj);

alert(obj.data.rs);
      })

    };
    $scope.registerBtnClicked=function(){
      $state.go('tab.register');
    }
  })

  //用户详情
  .controller('userInfoCtrl',function($scope,dataService){
    var requestUrl="getUserInfo";

    var userInfoParams={
      reqHead:{
        businessId:APIS.businessId,
        userId:APIS.userId,
        registrationId:APIS.registrationId,
        registerType:APIS.registerType
      },
      reqBody:{
        content:{
          //loginName:'',
          //password:''
          //person:[
          //  {name:'zhangshan',age:'1'},
          //  {name:'lisi',age:'2'}
          //]
        }
      }
    }
//console.log(userInfoParams);
    dataService.getData(requestUrl,userInfoParams).then(function(obj){
      if(obj.data.resultCode ==1){
        //$scope.user_Icon=obj.data.resultData.icon;
        $scope.user_name=obj.data.resultData.name;
        $scope.user_account=obj.data.resultData.account;
        if(obj.data.resultData.gender==1){
          $scope.user_gender="男";
        }else{
          $scope.user_gender="女";
        }
        $scope.user_age=obj.data.resultData.age;
        $scope.user_address=obj.data.resultData.address;
        $scope.user_comment=obj.data.resultData.comment;
      }else{
alert(obj.resultData);
      }
    });

  })

  //医院介绍
  .controller('hospitalIntroduceCtrl',function($scope,dataService,$state){

    $scope.hospitalIntroduce="医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介";
    $scope.departmentsSort="医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介";
    $scope.specialDepartments="医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介医院简介";



    var requestUrl="getHospitalInfo";
    var hospitalIntroduceParams={
      reqHead:{
        businessId:APIS.businessId,
        registrationId:APIS.registrationId,
        registerType:APIS.registerType
      },
      reqBody:{

      }
    }

    $scope.login=function(){
console.log("111");
      $state.go('tab.more.specialDepartment');
    }

    /*
    dataService.getData(requestUrl,hospitalIntroduceParams).then(function(data){
      if(data.resultCode == APIS.successCode){
        $scope.hospitalImg=data.resultData.icon;
        $scope.hospitalnfo=data.resultData.hospitalnfo;
        $scope.specialDepartments=data.resultData.specialDepartments;
        $scope.departmentsSort=data.resultData.departmentsSort;
        $scope.webAddress=data.resultData.webAddress;
        $scope.phone=data.resultData.phone;
        $scope.address=data.resultData.address;
        $scope.content=data.resultData.content;
      }
      else{
        alert(data.resultData);
      }

    })*/
  })
  //特色科室
  .controller('specialDepartments',function($scope,dataService,$state){
    var requestUrl="getSpecialDepartments";
    var specialDepartmentsParams={
      reqHead:{
        businessId:APIS.businessId,
        registrationId:APIS.registrationId,
        registerType:APIS.registerType
      },
      reqBody:{
        content:{

        }
      }
    }
    dataService.getData(requestUrl,specialDepartmentsParams).then(function(obj){
      if(data.resultCode == APIS.successCode){
          $scope.TotalCount=obj.data.resultData.TotalCount;
          $scope.Details=obj.data.resultData.Details;
      }
    })



  })

.controller('ChatDetailCtrl', function($scope, $stateParams, Chats) {
  $scope.chat = Chats.get($stateParams.chatId);
})

//个人中心
.controller('AccountCtrl', function($scope,dataService,$state) {

  var requestUrl="getpersonalCenter";
    var personalCenterParams={
      reqHead:{
        businessId:APIS.businessId,
        userId:"1",
        registrationId:APIS.registrationId,
        registerType:APIS.registerType
      },
      reqBody:{
        content:{

        }
      }
    }

    dataService.getData(requestUrl,personalCenterParams).then(function(obj){
      console.log(obj);
      console.log(obj.data.resultCode);
      if(obj.data.resultCode == APIS.successCode){

        //$scope.user_Icon=obj.data.resultData.Icon;
        $scope.user_account=obj.data.resultData.account;
        $scope.user_name=obj.data.resultData.name;
        if(obj.data.resultData.gender==1)
        {
          $scope.user_gender="男";
        }else{
          $scope.user_gender="女";
        }
        //$scope.user_dynamic=obj.data.resultData.dynamic;
      }
      else{
        alert(data.resultData);
      }

      $scope.td_1_clicked=function(){
alert("222");
        $state.go('tab.chats');
      }

    })
});
